#> Error in library(lterdatasampler): there is no package called 'lterdatasampler'
#> Error: object 'and_vertebrates' not found
The data
Data set and_vertebrates with measurements of a trout and 2 salamander species in different forest sections.
library(lterdatasampler)#> Error in library(lterdatasampler): there is no package called 'lterdatasampler'and_vertebrates#> Error: object 'and_vertebrates' not found
#> Error in library(paletteer): there is no package called 'paletteer'
#> Error: object 'g' not found
#> Error: object 'g' not found
scale_fill_* vs. scale_color_*
ggplot( and_vertebrates,aes(x = section,y = length_1_mm,color = unittype)) +geom_boxplot() +scale_color_paletteer_d(palette ="ggsci::default_uchicago" )#> Error: object 'and_vertebrates' not found
ggplot( and_vertebrates,aes(x = section,y = length_1_mm,fill = unittype)) +geom_boxplot() +scale_fill_paletteer_d(palette ="ggsci::default_uchicago" )#> Error: object 'and_vertebrates' not found
labs: Change axis and legend titles and add plot title
g <- g +labs(x ="Length [mm]",y ="Weight [g]",color ="Species",title ="Length-Weight relationship",subtitle ="There seems to be an exponential relationship",caption ="Data from the `lterdatasampler` package" )#> Error: object 'g' not foundg#> Error: object 'g' not found
labs: Change axis and legend titles and add plot title
theme_*: change appearance
ggplot2 offers many pre-defined themes that we can apply to change the appearance of a plot.
g +theme_classic()
#> Error: object 'g' not found
g +theme_bw()
#> Error: object 'g' not found
theme_*: change appearance
ggplot2 offers many pre-defined themes that we can apply to change the appearance of a plot.
g +theme_minimal()
#> Error: object 'g' not found
g +theme_dark()
#> Error: object 'g' not found
theme(): customize theme
You can manually change a theme or even create an entire theme yourself. The elements you can control in the theme are:
titles (plot, axis, legend, …)
labels
background
borders
grid lines
legends
If you want a full list of what you can customize, have a look at
?theme
Look here for an overview of the elements that you can change and the corresponding functions
theme(): customize theme
To edit a theme, just add another theme() layer to your plot.
You can set a global theme that will be applied to all ggplot objects in the current R session.
# Globally set theme_minimal as the default themetheme_set(theme_minimal())
Add this to the beginning of your script.
You can also specify some defaults, e.g. the text size:
theme_set(theme_minimal(base_size =16))
This is very practical if you want to achieve a consistent look, e.g. for a scientific journal.
ggsave()
A ggplot object can be saved on disk in different formats.
Without specifications:
# save plot g in img as my_plot.pdfggsave(filename ="img/my_plot.pdf", plot = g)# save plot g in img as my_plot.pngggsave(filename ="img/my_plot.png", plot = g)
Or with specifications:
# save a plot named g in the img directory under the name my_plot.png# with width 16 cm and height 9 cmggsave(filename ="img/my_plot.png",plot = g,width =16,heigth =9,units ="cm")